package com.gms.controller.usermanger;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gms.entity.user.Notice;
import com.gms.entity.user.User;
import com.gms.service.usermanager.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 公告的控制层
 */

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/toadd")
    public String toadd(){
        return "addnotice";
    }

    //添加公告
    @RequestMapping("/updatenotice")
    public String updateNotice(Notice notice,Model model){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        notice.setTime(time);
        notice.setState("正常");
        noticeService.add_notice(notice);
        String keyword = "";
        Page<Notice> page = noticeService.selectPage(1,keyword);
        model.addAttribute("page",page);
        return "noticelist";
    }

    //删除公告
    @RequestMapping("/deletenotice")
    public String deleteNotice(Integer noticeId,Model model){
        if(noticeId!=null) {
            noticeService.delete_notice(noticeId);
        }
        String keyword = "";
        Page<Notice> page = noticeService.selectPage(1,keyword);
        model.addAttribute("page",page);
        return "noticelist";
    }

    @RequestMapping("/querynotice")
    public String querynotice(Model model, @Nullable String keyword, @Nullable Integer pageNum){
        if (pageNum==null){
            pageNum=1;
        }if(!StringUtils.isEmpty(keyword)){
            model.addAttribute("keyword",keyword);
        }
        System.out.println(keyword);
        Page<Notice> page = noticeService.selectPage(pageNum,keyword);
        model.addAttribute("page",page);
        return "noticelist";
    }

    //所有公告
    @RequestMapping("/selectall")
    public String selectAll(Model model){
        String keyword = "";
        Page<Notice> page = noticeService.selectPage(1,keyword);
        model.addAttribute("page",page);
        return "noticelist";
    }

    @RequestMapping("/tonoticeuser")
    public String tonoticeuser(Model model){
        String keyword = "";
        Page<Notice> page = noticeService.selectPage(1,keyword);
        model.addAttribute("page",page);
        return "noticeuser";
    }

    @RequestMapping("/tonotice")
    public String tonotice(Model model,Integer noticeId){
        Notice notice = noticeService.select_one(noticeId);
        model.addAttribute("notice",notice);
        return "notice";
    }

}
